package com.hk.commons.utils.order;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.util.TimerTask;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hk.commons.JsonUtils;
import com.hk.commons.StringHelper;

public class HuochaiProductUpdInfo extends TimerTask {

	private Logger logger = LoggerFactory
			.getLogger(HuochaiProductUpdInfo.class);

	private int searchTimes = 3;

	private boolean continueFlag = true;

	private String orderId;

	private String url;

	public HuochaiProductUpdInfo(String orderId, String url) {
		this.orderId = orderId;
		this.url = url;
	}

	public void run() {
		if (searchTimes > 0 && continueFlag) {
			//调用次数减 1 
			searchTimes--;

			System.out.println("update run  开始运行" + "  searchTimes = "
					+ searchTimes + "   "
					+ new Timestamp(System.currentTimeMillis()));
			CloseableHttpClient httpClient = getHttpClient();
			CloseableHttpResponse response = null;
			try {
				HttpPost post = new HttpPost(this.url);
				
				// 创建参数列表、url格式编码
				String jsonStr = JsonUtils.toJson(new BasicNameValuePair(
						"orderId", orderId));
				StringEntity entity = new StringEntity(jsonStr, "UTF-8");
				entity.setContentType("application/json");
				post.setEntity(entity);

				logger.info("POST 请求...." + post.getURI() + "   参数为 : "
						+ post.getEntity().toString());
				try {
					// 执行请求
					response = httpClient.execute(post);
					HttpEntity respEntity = response.getEntity();

					String charset = EntityUtils.toString(respEntity);

					org.codehaus.jackson.JsonNode result = JsonUtils
							.toJsonObject(charset);
					
					//如果获得支付结果，则停止查询
					if (StringHelper.isNotEmpty(result.get("payStatus")
							.toString())) {
						continueFlag = false;
						//this.cancel();
					}

					logger.info("payStatus = " + result.get("payStatus")
							+ "  searchTimes = " + searchTimes);

				} finally {
					response.close();
				}

			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					closeHttpClient(httpClient);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	private CloseableHttpClient getHttpClient() {
		return HttpClients.createDefault();
	}

	private void closeHttpClient(CloseableHttpClient client) throws IOException {
		if (client != null) {
			client.close();
		}
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}
}
